<?php

/**
 * @description Recursively index the files in a folder
 *
 * This function tries hard to index all files in the specified folder
 * and all subfolders, recursively.
 *
 * @param string in_folder the folder in which to start searching
 * @param array string in_file the filename to look for
 * @return void
 */
function index_folder($in_folder,&$in_index)
{
	$cpath = getcwd().DIRECTORY_SEPARATOR;
	if (is_dir($in_folder))
	{
		if ($dh = opendir($in_folder))
		{
			while (($file = readdir($dh)) !== false ) {
				if ( $file != "." && $file != ".." )
				{
					$to_full_format = $in_folder.$file;
					if (is_dir($to_full_format))
					{
						index_folder($to_full_format."/",$in_index);
					}
					else
					{
						if ($file{0} >= 'A' && $file{0} <= 'Z' && substr($file,-9) != ".svn-base")
						{
							$in_index[$file] = $cpath.$to_full_format;
						}
					}
				}
			}
			return NULL;
		}
	}
}
$known_classes = array();
if (file_exists(AUTOLOAD_INDEX) && empty($_GET['reindex']))
{
	require_once(AUTOLOAD_INDEX);
}
else
{
	// Index core and plugin classes
	index_folder(CORE_CLASS_DIR, $known_classes);
	index_folder(APP_CLASS_DIR, $known_classes);
	index_folder(PLUGIN_DIR, $known_classes);

	$to = "<?php\n\$known_classes = array(\n";
	$classes = sizeof($known_classes);
	$class = 1;
	foreach ($known_classes as $classfile => $classlocation)
	{
		$to .= "'".$classfile."' => '".$classlocation."'".(($class < $classes) ? ",\n" : "\n");
		$class++;
	}
	$to .= ");\n?>";
	@file_put_contents(AUTOLOAD_INDEX, $to);

}

?>
